package com.daihou.utils;

import com.daihou.config.TheAuth;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

public class ReadTxtFile {
	private String url;
	Map<String, String> headers;
	public static Object[][] readTxtByPath(String strPath) throws Exception {
		Map<Integer, String> map = new HashMap<Integer, String>();
		/* 读取数据 */
		InputStream path = Thread.currentThread().getContextClassLoader().getResourceAsStream(strPath);
		BufferedReader br = new BufferedReader(new InputStreamReader(path));
		String lineTxt = null;
		int i = 1;
		while ((lineTxt = br.readLine()) != null) {
			map.put(i, lineTxt);
			i++;
		}
		int count = (i - 1) / 4;//i=25
		Object[][] data = new Object[count][];//count=6
		for (int j = 1, k = 0; j + 4 <= i; j = j + 4, k++) {
			data[k] = new Object[]{map.get(j), map.get(j + 1), map.get(j + 2), map.get(j + 3)};
		}
		br.close();
		return data;
	}
	@DataProvider(name = "data1")
	public Object[][] dp() throws Exception {
		Map<Integer, String> map = new HashMap<Integer, String>();
		/* 读取数据 */
		InputStream path = this.getClass().getResourceAsStream("/txtFiles/Lawyer.txt");
		BufferedReader br = new BufferedReader(new InputStreamReader(path));
		String lineTxt = null;
		int i = 1;
		while ((lineTxt = br.readLine()) != null) {
//			System.out.println(lineTxt);
			map.put(i, lineTxt);
			i++;
		}
		int count = (i - 1) / 3;
		Object[][] data = new Object[count][];
		for (int j = 1, k = 0; j + 4 < i; j = j + 3, k++) {
			data[k] = new Object[]{map.get(j), map.get(j + 1), map.get(j + 2)};
		}
		br.close();
		return data;
	}

	@Test(dataProvider = "data1")
	private void readData(String url, String body, String expected) throws IOException {
		InputStream path = this.getClass().getResourceAsStream("/txtFiles/common.txt");
		BufferedReader br = new BufferedReader(new InputStreamReader(path));
		String lineTxt =  br.readLine();

		headers = new HashMap<>();
		//设置请求头中的Authorization
		headers.put("Authorization", TheAuth.getAuth());
		//需要admin权限
		headers.put("X-UserName", "admin");
		try {
			String result = DHHttpUtil.post_with_String(lineTxt+url, body, headers).toJSONString();
			System.out.println("== " + result);
			Assert.assertTrue(result.contains(expected));

		} catch (Exception e) {
			System.err.println(e.getMessage());
		}
	}

	//	@Test
//	private void readFile() throws IOException {
//		url = DHConfigFile.getUrl(In_Pingfenka.JILU.toString());
//		headers = new HashMap<>();
//		//设置请求头中的Authorization
//		headers.put("Authorization", TheAuth.getAuth());
//		//需要admin权限
//		headers.put("X-UserName", "admin");
//
//		Map<Integer, String> map = new HashMap<Integer, String>();
//		/* 读取数据 */
//		try {
//			InputStream path = this.getClass().getResourceAsStream("/txtFile.txt");
//			BufferedReader br = new BufferedReader(new InputStreamReader(path));
//			String lineTxt = null;
//			int i = 1;
//			while ((lineTxt = br.readLine()) != null) {
//				System.out.println(lineTxt);
//				map.put(i, lineTxt);
//				i++;
//			}
//			for (int j = 1; j + 4 < i; j = j + 3) {
//				String result = DHHttpUtil.post_with_String(map.get(j), map.get(j + 1), headers).toJSONString();
//				Assert.assertTrue(result.contains(map.get(j + 2)));
//			}
//			br.close();
//		} catch (Exception e) {
//			System.err.println("读取文件异常 :" + e);
//		}
//	}
}